# -*- coding: utf-8 -*-
import xlrd
import pandas as pd

if __name__ == "__main__":
    excel_file = u'成绩单(2019-2020-1)数据库系统课程设计.xlsx'
    sheet_name = u'期末总评'
    saved_csv_file = 'database_practice.csv'
    start_rownumber = 5     #从第5行开始（起始行号1）
    output_columns = [2,6,7]    # 输出列号（起始列号1）

    data = xlrd.open_workbook(excel_file)
    # table = data.sheets()[0] 
    table = data.sheet_by_name(sheet_name)
    nrows = table.nrows  #获取该sheet中的有效行数
    # print(nrows)
    # 获取sheet1中第5行的数据
    start_idx = start_rownumber -1
    output_columns = [c-1 for c in output_columns]
    result_list=[]
    for x in range(start_idx, nrows):
        row_values = table.row_values(rowx=x)
        row_values = [row_values[idx] for idx in output_columns]
        currentList = []
        for v in row_values:
            if(isinstance(v, float)):
                currentList.append(unicode(round(v,0)))
                # print(u'浮点数:' + unicode(v))
            elif isinstance(v, unicode):
                currentList.append(v)
                # print(u'字符串' + unicode(v))
        result_list.append(currentList)

    df = pd.DataFrame(result_list)
    df.to_csv(saved_csv_file, encoding='gb2312', index=False)